package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo getPrice(Integer roomId) {
        return roomMapper.getPrice(roomId);
    }


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取房间中的智能设备及数据
     *
     * @param floorId
     * @return
     */
    @Override
    public R<List<RoomVo>> getRoomsWithDeviceByFloorId(Long floorId) {
        //1.根据楼层id查询基本信息()
        List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        if (CollectionUtils.isEmpty(roomVos)) {
            throw new BaseException("这个楼层没有数据!");
        }
        //2.拿到基本信息集合后,遍历集合,将设备数据添加进去(注意,包含房间设备数据和床位设备数据)设备数据从redis数据库中拿到
        //2.1房间设备数据
        roomVos.forEach(roomVo -> {
            List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
            deviceVos.forEach(deviceVo -> {
                String iotId = deviceVo.getIotId();
                String deviceDataStr = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                List<DeviceData> list = JSONUtil.toList(deviceDataStr, DeviceData.class);
                deviceVo.setDeviceDataVos(list);
            });
        });
        //2.床位设备数据
        roomVos.forEach(roomVo -> {
            List<BedVo> bedVos = roomVo.getBedVoList();
            bedVos.forEach(bedVo -> {
                bedVo.getDeviceVos().forEach(deviceVo -> {
                    String iotId = deviceVo.getIotId();
                    String deviceDataStr = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                    List<DeviceData> list = JSONUtil.toList(deviceDataStr, DeviceData.class);
                    deviceVo.setDeviceDataVos(list);
                });
            });
        });
        return R.ok(roomVos);
    }
}
